﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;

namespace BuildingDrawer
{
    class E_LineFrame
    {
        public float m_timestamp;

        public List<E_Line> m_line_added;
        public List<E_Line> m_line_removed;
        public List<E_Line> m_line_changed;

        public Hashtable m_line_current;

        public E_LineFrame(float in_time)
        {
            m_timestamp = in_time;
            m_line_added = new List<E_Line>();
            m_line_removed = new List<E_Line>();
            m_line_changed = new List<E_Line>();
            m_line_current = new Hashtable();
        }

        public E_LineFrame(float in_time,E_LineFrame in_frame)
        {
            m_timestamp = in_time;
            m_line_current = new Hashtable(in_frame.m_line_current);
        }

        public float getTime() { return m_timestamp; }

        public void AddLine(E_Line l)
        {
            m_line_current.Add(l.id,l);
        }

        public void RemoveLine(E_Line l)
        {
            m_line_current.Remove(l.id);
        }

        /// <summary>
        /// update the line data
        /// </summary>
        /// <param name="l"></param>
        public void ChangeLine(E_Line l)
        {
            m_line_current.Remove(l.id);
            m_line_current.Add(l.id, l);
        }

        // id p1id p2id all the same
        private Boolean isContainLine(E_Line l)
        {
            foreach (DictionaryEntry de in m_line_current)
            {
                E_Line m_l = (E_Line)de.Value;
                if (l.isEqual(m_l))
                {
                    return true;
                }
            }
            return false;
        }

        // id is same is ok
        private Boolean isContainLineID(E_Line l)
        {
            if (m_line_current.ContainsKey(l.id))
            {
                return true;
            }
            return false;
        }

        public void compareLineFrame(E_LineFrame last_frame)
        {
            foreach (DictionaryEntry de in last_frame.m_line_current)
            {
                E_Line l = (E_Line)de.Value;

                if (!this.isContainLineID(l))
                {
                    this.m_line_removed.Add(l);
                }
            }

            foreach (DictionaryEntry de in this.m_line_current)
            {
                E_Line m_l = (E_Line)de.Value;

                if (!last_frame.isContainLine(m_l)) // last frame does not contain the same line
                {
                    if (!last_frame.isContainLineID(m_l))
                    {
                        this.m_line_added.Add(m_l);
                    }
                    else
                    {
                        this.m_line_changed.Add(m_l);
                    }
                }
            }
        }

        //public int isEqual(E_LineFrame frame_in)
        //{
        //    E_Line[] lines_sorted = frame_in.sortDes();
        //    int length = lines_sorted.Length;
        //    for (int i = 0; i < length; i++)
        //    {
        //        if (!m_line_current[i].isEqual(lines_sorted[i]))
        //        {
        //            return i + 1;
        //        }
        //    }
        //    return 0;
        //}

        //private E_Line[] sortDes()
        //{
        //    int s = m_line_current.Count;
        //    E_Line[] lines_sorted = new E_Line[s];

        //    foreach (E_Line l in m_line_current)
        //    {
        //        int p = l.id;
        //        lines_sorted[s - p] = l;
        //    }

        //    return lines_sorted;
        //}
    }
}
